package Framework
{
	import mx.utils.StringUtil;

	public class StringUtil
	{
		public static function trim(input:String):String{
			return mx.utils.StringUtil.trim(input);
		}
		private static function leftPad(source:String, targetLength:int, padChar:String = " "):String
		{
			if (source.length < targetLength)
			{
				var padding:String = "";
				
				while (padding.length + source.length < targetLength)
					padding += padChar;
				
				return padding + source;
			}
			
			return source;
		}		
		private static function rightPad(source:String, targetLength:int, padChar:String = " "):String
		{
			while (source.length < targetLength)
				source += padChar;
			
			return source;
		}		
		private static function intString(value:int):String{
			if(value>=0 && value<10)
				return "0" + value
			return String(value)
		} 
		static public function sprintf(format:String, ... args):String
		{
			var result:String = "";
			var next:*;
			var str:String;
			
			var length:int = format.length;
			for (var i:int = 0; i < length; i++)
			{
				var c:String = format.charAt(i);
				
				if (c == "%") 
				{
					var pastFieldWidth:Boolean = false;
					var pastFlags:Boolean = false;
					
					var flagAlternateForm:Boolean = false;
					var flagZeroPad:Boolean = false;
					var flagLeftJustify:Boolean = false;
					var flagSpace:Boolean = false;
					var flagSign:Boolean = false;
					
					var fieldWidth:String = "";
					var precision:String = "";
					
					c = format.charAt(++i);
					
					while (c != "d"
						&& c != "i"
						&& c != "o"
						&& c != "u"
						&& c != "x"
						&& c != "X"
						&& c != "f"
						&& c != "F"
						&& c != "c"
						&& c != "s"
						&& c != "%")
					{
						if (!pastFlags)
						{
							if (!flagAlternateForm && c == "#")
								flagAlternateForm = true;
							else if (!flagZeroPad && c == "0")
								flagZeroPad = true;
							else if (!flagLeftJustify && c == "-")
								flagLeftJustify = true;
							else if (!flagSpace && c == " ")
								flagSpace = true;
							else if (!flagSign && c == "+")
								flagSign = true;
							else
								pastFlags = true;
						}
						
						if (!pastFieldWidth && c == ".")
						{
							pastFlags = true;
							pastFieldWidth = true;
							
							c = format.charAt(++i);
							continue;
						}
						
						if (pastFlags)
						{
							if (!pastFieldWidth)
								fieldWidth += c;
							else
								precision += c;
						}
						
						c = format.charAt(++i);
					}
					
					switch (c)
					{
						case "d":
						case "i":
							next = args.shift();
							str = String(Math.abs(int(next)));
							
							if (precision != "")
								str = leftPad(str, int(precision), "0");
							
							if (int(next) < 0)
								str = "-" + str;
							else if (flagSign && int(next) >= 0)
								str = "+" + str;
							
							if (fieldWidth != "")
							{
								if (flagLeftJustify)
									str = rightPad(str, int(fieldWidth));
								else if (flagZeroPad && precision == "")
									str = leftPad(str, int(fieldWidth), "0");
								else
									str = leftPad(str, int(fieldWidth));
							}
							
							result += str;
							break;
						
						case "o":
							next = args.shift();
							str = uint(next).toString(8);
							
							if (flagAlternateForm && str != "0")
								str = "0" + str;
							
							if (precision != "")
								str = leftPad(str, int(precision), "0");
							
							if (fieldWidth != "")
							{
								if (flagLeftJustify)
									str = rightPad(str, int(fieldWidth));
								else if (flagZeroPad && precision == "")
									str = leftPad(str, int(fieldWidth), "0");
								else
									str = leftPad(str, int(fieldWidth));
							}
							
							result += str;
							break;
						
						case "u":
							next = args.shift();
							str = uint(next).toString(10);
							
							if (precision != "")
								str = leftPad(str, int(precision), "0");
							
							if (fieldWidth != "")
							{
								if (flagLeftJustify)
									str = rightPad(str, int(fieldWidth));
								else if (flagZeroPad && precision == "")
									str = leftPad(str, int(fieldWidth), "0");
								else
									str = leftPad(str, int(fieldWidth));
							}
							
							result += str;
							break;
						
						case "X":
							var capitalise:Boolean = true;
						case "x":
							next = args.shift();
							str = uint(next).toString(16);
							
							if (precision != "")
								str = leftPad(str, int(precision), "0");
							
							var prepend:Boolean = flagAlternateForm && uint(next) != 0;
							
							if (fieldWidth != "" && !flagLeftJustify
								&& flagZeroPad && precision == "")
								str = leftPad(str, prepend
									? int(fieldWidth) - 2 : int(fieldWidth), "0");
							
							if (prepend)
								str = "0x" + str;
							
							if (fieldWidth != "")
							{
								if (flagLeftJustify)
									str = rightPad(str, int(fieldWidth));
								else
									str = leftPad(str, int(fieldWidth));
							}
							
							if (capitalise)
								str = str.toUpperCase();
							
							result += str;
							break;
						
						case "f":
						case "F":
							next = args.shift();
							str = Math.abs(Number(next)).toFixed(
								precision != "" ?  int(precision) : 6);
							
							if (int(next) < 0)
								str = "-" + str;
							else if (flagSign && int(next) >= 0)
								str = "+" + str;
							
							if (flagAlternateForm && str.indexOf(".") == -1)
								str += ".";
							
							if (fieldWidth != "")
							{
								if (flagLeftJustify)
									str = rightPad(str, int(fieldWidth));
								else if (flagZeroPad && precision == "")
									str = leftPad(str, int(fieldWidth), "0");
								else
									str = leftPad(str, int(fieldWidth));
							}
							
							result += str;
							break;
						
						case "c":
							next = args.shift();
							str = String.fromCharCode(int(next));
							
							if (fieldWidth != "")
							{
								if (flagLeftJustify)
									str = rightPad(str, int(fieldWidth));
								else
									str = leftPad(str, int(fieldWidth));
							}
							
							result += str;
							break;
						
						case "s":
							next = args.shift();
							str = String(next);
							
							if (precision != "")
								str = str.substring(0, int(precision));
							
							if (fieldWidth != "")
							{
								if (flagLeftJustify)
									str = rightPad(str, int(fieldWidth));
								else
									str = leftPad(str, int(fieldWidth));
							}
							
							result += str;
							break;
						
						case "%":
							result += "%";
					}
				}
				else
				{
					result += c;
				}
			}
			
			return result;
		}
	}
}